package reduce.fz.com.reduce.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.FindTeamActivity;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.StoryCard;
import reduce.fz.com.reduce.bean.TeamCategory;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.HttpUtils;

/**
 * 小组分类接口
 */
public class TeamCategoryFragment extends BaseListFragment<TeamCategoryFragment.TeamCategoryViewHolder, TeamCategory> implements FragmentCacheSupport {

    private LayoutInflater layoutInflater;
    private Context context;

    @Override
    public String getCacheName() {
        return "TEAMCATEGORYFRAGMENT";
    }

    @Override
    public String getFragmentTitle() {
        return "分类";
    }

    public interface CategoryListener {
        public void onClickCategory(TeamCategory category);
    }

    public static TeamCategoryFragment create() {
        TeamCategoryFragment fragment = new TeamCategoryFragment();
        return fragment;
    }


    public TeamCategoryFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        this.context = view.getContext();
        layoutInflater = LayoutInflater.from(view.getContext());
        return view;
    }

    /**
     * 单项
     *
     * @param parent
     * @return
     */
    @Override
    public TeamCategoryViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_team_category, parent, false);
        return new TeamCategoryViewHolder(view, new CategoryListener() {
            @Override
            public void onClickCategory(TeamCategory category) {
                Intent intent = new Intent(context, FindTeamActivity.class);
                intent.putExtra("categoryid", category.categoryid);
                startActivity(intent);
            }
        });
    }
    /**
     * foot翻页提示
     *
     * @param view
     * @return
     */
    @Override
    public TeamCategoryViewHolder createfootViewHolder(View view) {
        TeamCategoryViewHolder footHolder = new TeamCategoryViewHolder(view);
        return footHolder;
    }

    /**
     * 请求url
     *
     * @return
     */
    @Override
    public String setUrlBeforeRequest(List<TeamCategory> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_team_category, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        //PAGENO分页方式
        return Pagination.TYPE_PAGE_NO;
    }


    //请求URL
    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<TeamCategory>>(new TypeReference<Result<List<TeamCategory>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                loadingDialog.dismiss();
            }
            @Override
            public void responseInfo(Result<List<TeamCategory>> data) {
                addResult(data);
            }
        });
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
        return layoutManager;
    }

    public static class TeamCategoryViewHolder extends BaseRecycleViewHolder<TeamCategory> {
        //是否是第一行
        boolean isFirstLine = false;
        //是否是第一列
        boolean isFirstRow = false;
        private View itemView;
        private NetworkImageView ivTeamCategory;
        private TextView mTvTitle;
        private CategoryListener categoryListener;

        int cardmargin = context.getResources().getDimensionPixelSize(R.dimen.double_card_margin);

        public TeamCategoryViewHolder(View itemView, CategoryListener categoryListener) {
            this(itemView);
            this.categoryListener = categoryListener;

        }

        public TeamCategoryViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            ivTeamCategory = (NetworkImageView) itemView.findViewById(R.id.iv_category);
            mTvTitle = (TextView) itemView.findViewById(R.id.tv_title);
        }

        @Override
        public void renderView(final TeamCategory data, int position) {
            checkPosition(position);
            resetPadding(position);
            // mTvTitle.setText(data.title);
            ivTeamCategory.setImageUrl(data.iconUrl, HttpUtils.getImageLoader());
            ivTeamCategory.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    categoryListener.onClickCategory(data);
                }
            });

        }

        public void checkPosition(int position) {
            //默认非第一行，非第一列
            if (position <= 1)
                isFirstLine = true;
            if (position % 2 == 0)
                isFirstRow = true;
        }

        public void resetPadding(int position) {
            int paddingtop = 0;
            int paddingbottom = 0;
            int paddingleft = 0;
            int paddingright = 0;
            if (isFirstLine) {
                paddingtop = cardmargin / 2;
                paddingbottom = 0;
            } else {
                paddingtop = cardmargin;
                paddingbottom = 0;
            }
            if (isFirstRow) {
                paddingleft = cardmargin / 2;
                paddingright = 0;
            } else {
                paddingright = cardmargin / 2;
                paddingleft = cardmargin;
            }
            itemView.setPadding(paddingleft, paddingtop, paddingright, paddingbottom);


        }

    }


}
